package server.core;

import server.Context;
import server.deploy.FilterDef;
import server.deploy.FilterMap;

import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import java.util.HashMap;

public class StandardContext extends ContainerBase implements Context {
    //后续写
    public FilterConfig findFilterConfig(String name) {
        synchronized (filterConfigs) {
            return filterConfigs.get(name);
        }
    }
    public void initial() {
        FilterDef filterDef1 = new FilterDef();
        filterDef1.setFilterClass("Filter1");
        filterDef1.setFilterName("F1");
        filterDef1.addInitParameter("f1", "k1");
        filterDef1.addInitParameter("f2", "k2");

        FilterDef filterDef2 = new FilterDef();
        filterDef2.setFilterClass("Filter2");
        filterDef2.setFilterName("F2");

        try {
            ApplicationFilterConfig filterConfig1 = new ApplicationFilterConfig(this, filterDef1);
            ApplicationFilterConfig filterConfig2 = new ApplicationFilterConfig(this, filterDef2);
            filterConfigs.put("F1", filterConfig1);
            filterConfigs.put("F2", filterConfig2);
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
    @Override
    public ServletContext getServletContext() {
        //后续写
        return null;
    }

    @Override
    public boolean getAvailable() {
        //后续写
        return true;
    }

    @Override
    public FilterMap[] findFilterMaps() {
        //后续写
        FilterMap[] filterMaps = new FilterMap[2];
        FilterMap f1 = new FilterMap();
        f1.setFilterName("F1");
        f1.setUrlPattern("/*");

        FilterMap f2 = new FilterMap();
        f2.setFilterName("F2");
        f2.setServletName("M");
        filterMaps[0] = f1;
        filterMaps[1] = f2;
        return filterMaps;
    }

    private static final String info = "server.core.StandardContext/1.0";
    private HashMap<String, FilterConfig> filterConfigs = new HashMap<>();

    //getter---------------------------------------------------------------------------

    //setter---------------------------------------------------------------------------

    //实现Container接口---------------------------------------------------------------------------
    @Override
    public String getInfo() {
        return info;
    }
    //实现Context接口---------------------------------------------------------------------------



}
